package com.aurora.source.base;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.aurora.source.R;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @author: ww
 * @description:
 * @date: 2018/10/11
 */
public class WebViewActivity extends BaseWhiteToolbarActivity {
    @BindView(R.id.webview)
    WebView mWebView;

    @Override
    public BasePresenter attach() {
        return null;
    }

    @Override
    public int bindLayout() {
        return R.layout.activity_web_view;
    }

    @Override
    public void initData(@Nullable Bundle bundle) {
        simpleToolbar.setMainTitle(getIntent().getStringExtra("title"));
        simpleToolbar.setLeftVisibility(View.VISIBLE);
    }

    @Override
    public void initView(Bundle savedInstanceState, View contentView) {
        mWebView.getSettings().setJavaScriptEnabled(true);
        mWebView.setWebViewClient(new SampleWebViewClient());

        mWebView.getSettings().setUseWideViewPort(true);
        mWebView.getSettings().setLoadWithOverviewMode(true);
        mWebView.getSettings().setAllowFileAccess(true);
        mWebView.getSettings().setDomStorageEnabled(true);
        mWebView.loadUrl(getIntent().getStringExtra("url"));
    }

    /**
     * 返回监听
     */
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if ((keyCode == KeyEvent.KEYCODE_BACK) && mWebView != null
                && mWebView.canGoBack()) {
            mWebView.goBack();
            return true;
        } else if (keyCode == KeyEvent.KEYCODE_BACK) {
            hideSoftInput();
            finish();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }


    private class SampleWebViewClient extends WebViewClient {


        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {

            super.onPageStarted(view, url, favicon);

        }


        @Override
        public void onPageFinished(WebView view, String url) {

            super.onPageFinished(view, url);

        }


        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {

            Log.e("URL",url);
            return super.shouldOverrideUrlLoading(view, url);


        }


    }

    @Override
    public void doBusiness() {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }
}
